package com.acmedcare.framework.exchange.center.core.consumer;

import com.acmedcare.framework.exchange.center.core.PluginMetadata;
import com.acmedcare.framework.exchange.center.core.metadata.IDataConsumer;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
import xyz.vopen.cartier.classpathscanner.scanner.ScanResult;

@Slf4j
public class DefaultConsumerResolver implements IConsumerResolver {

  @Override
  public void resolve(ConcurrentHashMap<String, ConsumerMetadata> consumerMap,
      PluginMetadata metadata) {

    ScanResult scanResult = metadata.getScanResult();
    ClassLoader classLoader = metadata.getClassLoader();
    List<String> consumers = scanResult.getNamesOfClassesImplementing(IDataConsumer.class);
    if (consumers.isEmpty()) {

      return;
    }

    consumers.forEach(c -> {

      ConsumerMetadata consumerMetadata = new ConsumerMetadata();
      try {
        consumerMetadata.setConsumerClass(classLoader.loadClass(c));
        consumerMap.put(c.toLowerCase(), consumerMetadata);
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      }
    });
  }
}
